# -*- coding: utf-8 -*-
import cherrypy as _cp  
import os.path  
import pymongo 
from backend.t import TweetView 
from backend.msg import MsgView 
from backend.stock import StockView 
from backend.user import UserView     
from pymongo import Connection

current_dir = os.path.dirname(__file__) 
conn = Connection()    
db_t = conn['t']
db_m = conn['msg'] 
db_s = conn['stock'] 
root = TweetView(db_t) 
root.msg = MsgView(db_m)
root.stk = StockView(db_s)
root.u = UserView(db_t)
app_config = {
    '/': {'tools.staticdir.root': current_dir}, 
    '/static': {'tools.staticdir.on': True,
                'tools.staticdir.dir': 'www/static'
            }, 
    '/files': {'tools.staticdir.on': True,
                'tools.staticdir.dir': 'var/files'
            }, 
} 
_cp.tree.mount(root,config=app_config)
if __name__ == '__main__':
    #global config
    global_config = {
        'server.socket_host':'127.0.0.1',
        'server.socket_port':8080, 
        'server.protocol_version':'HTTP/1.1',
        'tools.sessions.on':True, 
        'log.error_file':'var/logs/error.log',
        'log.access_file':'var/logs/access.log', 
        'tools.log_tracebacks.on':True, 
        'tools.session_auth.on':True,     
        'tools.session_auth.check_username_and_password': root.check_username_and_password,
        'avatar.path' :  os.path.join(current_dir,'www','static','avatar'), 
    }
    _cp.config.update(config=global_config)
    
    _cp.engine.start()
    _cp.engine.block()